package com.alianzamedica.controllers.medic;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.objectsearch.sqlsearch.ObjectSearch;
import org.objectsearch.web.tools.Converter;
import org.w3c.dom.Document;

import com.alianzamedica.businessobject.Doctor;
import com.alianzamedica.tools.Enviroment;
import com.alianzamedica.view.MedicEditForm;

/**
 * @author Carlos
 * 
 */
public class MedicSaveAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Enviroment env = Enviroment.getInstance();
		Document doc = env.getDocument();
		ObjectSearch search = new ObjectSearch(doc,
				"com.alianzamedica.connection.ConnectionImpl");
		MedicEditForm medicForm = (MedicEditForm) form;
		Doctor doctor = new Doctor();
		doctor.setId(Converter.string2Integer(medicForm.getId()));
		doctor.setFirstName(medicForm.getFirstName());
		doctor.setLastName(medicForm.getLastName());
		doctor.setEmail(medicForm.getEmail());
		doctor.setPassword(medicForm.getPassword());
		doctor.setUniversity(medicForm.getUniversity());
		doctor.setSpeciality(medicForm.getSpeciality());
		doctor.setCell(medicForm.getCell());
		doctor.setStateCell(medicForm.getStateCell());
		doctor.setCertificationNO(medicForm.getCertificationNO());
		doctor.setPhoneNo(medicForm.getPhoneNo());
		doctor.setLocation(medicForm.getLocation());
		doctor.setRadio(medicForm.getRadio());
		doctor.setHour(medicForm.getHour());
		doctor.setFullName(doctor.getFirstName() + " " + doctor.getLastName());
		doctor.setActive(1);
		doctor.setProfile(Converter.string2Integer(medicForm.getProfile()));
		String mensaje = "";
		if (doctor.getId() == null) {
			search.insertObject(doctor);
			mensaje = "Se ha logrado agregar el registro de manera exitosa";
		} else {
			search.updateObject(doctor);
			mensaje = "Se ha logrado actualizar el registro de manera exitosa";
		}
		String retorno = "/admin/medic/list.do";
		request.setAttribute("mensaje", mensaje);
		request.setAttribute("linkRetorno", retorno);

		return mapping.findForward("success");
	}

}
